package com.g2room.tools.mybatisTool;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.g2room.tools.mybatisTool.to.ClazzTo;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * 使用Freemarker模板生成文件
 * @author Administrator
 *
 */
public class CreateFileByFtl {
	// 保存文件路径
	private static String xmlFilePath = "D:/workspace/code/eclipse/self/self/src/main/resources/";
	private static String basePackage= "D:/workspace/code/eclipse/self/self/src/main/java/com/carry6/self/";
	public static String tmplFlord= "D:/workspace/code/eclipse/JavaProject/src/com/g2room/tools/mybatisTool/tmpl";
	
	public static void main(String[] args) throws IOException{
		List<ClazzTo> clazzTos = TableInfo.getTableInfo();
		
		 @SuppressWarnings("deprecation")
		Configuration cfg = new Configuration();
		 cfg.setDirectoryForTemplateLoading(new File(tmplFlord));  
        
		 createPo(clazzTos, cfg);
		 createMybatisXmlMapper(clazzTos, cfg);
		 createMybatisCfg(clazzTos, cfg);
		 createMapper(clazzTos, cfg);
		 createService(clazzTos, cfg);
    }
	
	public static void createPo(List<ClazzTo> clazzTos, Configuration cfg) {
        try {
            Template tmpl = cfg.getTemplate("GenPo.tmpl");
            for(ClazzTo clazzTo : clazzTos) {
            	StringWriter out = null;
	            try {
	            	out = new StringWriter();
	            	tmpl.process(clazzTo, out);
	            	// 生成Mapper文件
					createFile(out.toString(), basePackage, "model/lol/po/" + clazzTo.getPoName() + ".java");
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						if(null != out) out.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
            }
        } catch (IOException e) {
        	e.printStackTrace();
        }
	}
	public static void createMybatisXmlMapper(List<ClazzTo> clazzTos, Configuration cfg) {
		try {
			Template tmpl = cfg.getTemplate("GenMapper.xml");
			for(ClazzTo clazzTo : clazzTos) {
				StringWriter out = null;
				try {
					out = new StringWriter();
					tmpl.process(clazzTo, out);
					// 生成Mapper文件
					createFile(out.toString(), xmlFilePath, "mybatis/" + clazzTo.getXmlName() + ".xml");
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						if(null != out) out.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void createMybatisCfg(List<ClazzTo> clazzTos, Configuration cfg) {
        // 生成配置文件
        try {
            Template tmpl = cfg.getTemplate("mybatis-config.xml");
	            StringWriter cfgTmplOut = null;
	            try {
	            	cfgTmplOut = new StringWriter();
	            	Map<String, List<ClazzTo>> clazzToMap = new HashMap<String, List<ClazzTo>>();
	            	clazzToMap.put("mybatisCfgs", clazzTos);
	            	tmpl.process(clazzToMap, cfgTmplOut);
	            	// 生成Mybatis Config文件
	            	createFile(cfgTmplOut.toString(), xmlFilePath, "mybatis-config.xml");
	            } catch (Exception e) {
	            	e.printStackTrace();
	            } finally {
					try {
						if(null != cfgTmplOut) cfgTmplOut.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
        } catch (IOException e) {
        	e.printStackTrace();
        }
	}
	
	/**
	 * 创建Mapper的Java文件
	 * @param clazzTos
	 * @param cfg
	 */
	public static void createMapper(List<ClazzTo> clazzTos, Configuration cfg) {
        // 生成配置文件
        try {
            Template tmpl = cfg.getTemplate("GenMapper.tmpl");
            for(ClazzTo clazzTo : clazzTos) {
            	StringWriter out = null;
	            try {
	            	out = new StringWriter();
	            	tmpl.process(clazzTo, out);
	            	// 生成Mapper文件
					createFile(out.toString(), basePackage, "mapper/" + clazzTo.getMapperName() + ".java");
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						if(null != out) out.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
            }
        } catch (IOException e) {
        	e.printStackTrace();
        }
	}
	
	/**
	 * 创建Mapper的Java文件
	 * @param clazzTos
	 * @param cfg
	 */
	public static void createService(List<ClazzTo> clazzTos, Configuration cfg) {
		// 生成配置文件
		try {
			Template tmpl = cfg.getTemplate("GenService.tmpl");
			for(ClazzTo clazzTo : clazzTos) {
				StringWriter out = null;
				try {
					out = new StringWriter();
					tmpl.process(clazzTo, out);
					// 生成Mapper文件
					createFile(out.toString(), basePackage, "service/" + clazzTo.getServiceName() + ".java");
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						if(null != out) out.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void createFile(String content, String filePath, String fileName) throws Exception {
		String path = filePath + "//" + fileName;
		if(path.contains("/")) {
			filePath = path.substring(0, path.lastIndexOf("/"));
		}
        //得到写文件路径
        File file = new File(filePath);
        //如果目录不存在，则创建目录
        if(!file.exists())  file.mkdirs();
        //写文件
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(path),"utf-8");
        out.write(content);
        out.flush();
        out.close();
    }
	
}
